package cn.com.anypay.manager.lakala.exception;

import lombok.Getter;

/**
 * 拉卡拉API异常
 *
 * @author Claude
 * @date 2024-01-15
 */
@Getter
public class LakalaApiException extends RuntimeException {

    /**
     * 错误码
     */
    private final String code;

    /**
     * 拉卡拉返回的原始错误码
     */
    private final String lakalaCode;

    /**
     * 拉卡拉返回的原始错误信息
     */
    private final String lakalaMsg;

    /**
     * 请求的订单号
     */
    private final String orderNo;

    /**
     * 请求的API路径
     */
    private final String apiPath;

    /**
     * 构造函数
     *
     * @param code 系统错误码
     * @param message 错误信息
     * @param lakalaCode 拉卡拉错误码
     * @param lakalaMsg 拉卡拉错误信息
     */
    public LakalaApiException(String code, String message, String lakalaCode, String lakalaMsg) {
        super(message);
        this.code = code;
        this.lakalaCode = lakalaCode;
        this.lakalaMsg = lakalaMsg;
        this.orderNo = null;
        this.apiPath = null;
    }

    /**
     * 构造函数（带订单号和API路径）
     *
     * @param code 系统错误码
     * @param message 错误信息
     * @param lakalaCode 拉卡拉错误码
     * @param lakalaMsg 拉卡拉错误信息
     * @param orderNo 订单号
     * @param apiPath API路径
     */
    public LakalaApiException(String code, String message, String lakalaCode, String lakalaMsg,
                              String orderNo, String apiPath) {
        super(message);
        this.code = code;
        this.lakalaCode = lakalaCode;
        this.lakalaMsg = lakalaMsg;
        this.orderNo = orderNo;
        this.apiPath = apiPath;
    }

    /**
     * 构造函数（带原因）
     *
     * @param code 系统错误码
     * @param message 错误信息
     * @param cause 原因
     */
    public LakalaApiException(String code, String message, Throwable cause) {
        super(message, cause);
        this.code = code;
        this.lakalaCode = null;
        this.lakalaMsg = null;
        this.orderNo = null;
        this.apiPath = null;
    }

    /**
     * 创建参数错误异常
     */
    public static LakalaApiException paramError(String message) {
        return new LakalaApiException("LAKALA_PARAM_ERROR", message, null, null);
    }

    /**
     * 创建签名错误异常
     */
    public static LakalaApiException signError(String message) {
        return new LakalaApiException("LAKALA_SIGN_ERROR", message, null, null);
    }

    /**
     * 创建API调用失败异常
     */
    public static LakalaApiException apiError(String lakalaCode, String lakalaMsg, String orderNo, String apiPath) {
        String message = String.format("拉卡拉API调用失败: [%s] %s", lakalaCode, lakalaMsg);
        return new LakalaApiException("LAKALA_API_ERROR", message, lakalaCode, lakalaMsg, orderNo, apiPath);
    }

    /**
     * 创建网络错误异常
     */
    public static LakalaApiException networkError(String message, Throwable cause) {
        return new LakalaApiException("LAKALA_NETWORK_ERROR", "拉卡拉API网络错误: " + message, cause);
    }

    /**
     * 创建解析错误异常
     */
    public static LakalaApiException parseError(String message) {
        return new LakalaApiException("LAKALA_PARSE_ERROR", "拉卡拉响应解析错误: " + message, null, null);
    }
}